﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using DashFire;
using UnityEngine;
using UnityEditor;
using System.Security.Cryptography;

namespace DashFire.Assetbundle
{
  internal class ExportRes_Deployer
  {
    internal static string s_ResCommitSearchPattern = "*.ab;*.txt";
    internal static string s_ResCommitBuildInPath = "Assets/StreamingAssets/ResFile";
    internal static string s_ResCommitCachePath = "ResFile";
    internal static string s_ResStreamingAssetPath = "Assets/StreamingAssets/Client;Assets/StreamingAssets/Public;Assets/StreamingAssets/list.txt;";
    internal static string s_ResUIAssetPath = "Assets/UI/Resources;";

    #region Commit
    internal static bool CommitBuildInResources()
    {
      ExportConfig.Log("--------------------------------------------------------------");
      ExportConfig.Log("ExportRes_Deployer Start...");

      CleanBuildInRes();
      if (ExportConfig.s_IsDeployClean) {
        CleanStreamingAssets();
        CleanUIAssets();
      }
      string srcDir = ExportConfig.GetPlatformPath();
      if (!Directory.Exists(srcDir)) {
        ExportConfig.Log("ExportRes_Deployer.CopyBuildInResources failed srcPath:" + srcDir);
        return false;
      }
      string destDir = s_ResCommitBuildInPath;
      if (!Directory.Exists(destDir)) {
        Directory.CreateDirectory(destDir);
      }

      ExportConfig.Log("ExportRes_Deployer.CopyDir from:{0} to:{1}", srcDir, destDir);
      if (!ExportConfig.CopyDir(srcDir, destDir)) {
        ExportConfig.Log("ExportRes_Deployer.CopyDir falied. from:{0} to:{1}", srcDir, destDir);
        return false;
      }

      AssetDatabase.Refresh();
      ExportConfig.Log("ExportRes_Deployer.CopyBuildInResources Done");
      return true;
    }
    #endregion
    #region Clean
    internal static bool CleanCache()
    {
      bool ret = false;
      try {
        Caching.CleanCache();
        string resCachePath = ExportConfig.PathCombine(Application.persistentDataPath, s_ResCommitCachePath);
        if (Directory.Exists(resCachePath)) {
          ExportConfig.DeleteDirectory(resCachePath);
        }
        string resSheetPath = ExportConfig.PathCombine(Application.persistentDataPath, ExportRes_Sheet.s_SheetCachePath);
        if (Directory.Exists(resSheetPath)) {
          ExportConfig.DeleteDirectory(resSheetPath);
        }
        ret = true;
        ExportConfig.Log("ResBuildProcesser.CleanCache Done");
      } catch (System.Exception ex) {
        ExportConfig.Log("ResBuildProcesser.CleanCache failed! ex:" + ex);
        ret = false;
      }
      return ret;
    }
    internal static bool CleanBuildInRes()
    {
      string buildinPath = ExportConfig.GetFilePathAbs(s_ResCommitBuildInPath);
      if (Directory.Exists(buildinPath)) {
        AssetDatabase.MoveAssetToTrash(s_ResCommitBuildInPath);
      }
      ExportConfig.Log("ResBuildProcesser.CleanBuildInRes Done");
      return true;
    }
    internal static bool CleanStreamingAssets()
    {
      string[] tStreamingAssetDirs = s_ResStreamingAssetPath.Split(ExportConfig.s_ConfigSplit,
        StringSplitOptions.RemoveEmptyEntries);
      if (tStreamingAssetDirs != null && tStreamingAssetDirs.Length > 0) {
        foreach (string saDir in tStreamingAssetDirs) {
          AssetDatabase.MoveAssetToTrash(saDir);
        }
      }
      return true;
    }
    internal static bool CleanUIAssets()
    {
      if (!ExportConfig.s_IsSupportUIUpdage) {
        ExportConfig.Log("ExportRes_Deployer.CleanUIAssets Support Closed.");
        return true;
      }
      string[] tUIAssetDirs = s_ResUIAssetPath.Split(ExportConfig.s_ConfigSplit,
        StringSplitOptions.RemoveEmptyEntries);
      if (tUIAssetDirs != null && tUIAssetDirs.Length > 0) {
        foreach (string saDir in tUIAssetDirs) {
          AssetDatabase.MoveAssetToTrash(saDir);
        }
      }
      return true;
    }
    internal static bool CleanOutputDir()
    {
      string outputPath = ExportConfig.GetFilePathAbs(ExportConfig.s_OutputPath);
      if (Directory.Exists(outputPath)) {
        ExportConfig.DeleteDirectory(outputPath);
      }
      ExportConfig.Log("ResProcess.CleanOutputDir Done");
      return true;
    }
    internal static bool CleanAll()
    {
      if (ExportRes_Deployer.CleanCache()
          && ExportRes_Deployer.CleanBuildInRes()
          && ExportRes_Deployer.CleanStreamingAssets()
          && ExportRes_Deployer.CleanOutputDir()) {
        return true;
      }
      return false;
    }
    internal static bool CleanOutputResDir()
    {
      string outputPath = ExportConfig.GetFilePathAbs(ExportConfig.GetPackagePath());
      if (Directory.Exists(outputPath)) {
        ExportConfig.DeleteDirectory(outputPath);
      }
      string outputTmpPath = ExportConfig.GetFilePathAbs(ExportConfig.GetTmpPackagePath());
      if (Directory.Exists(outputTmpPath)) {
        ExportConfig.DeleteDirectory(outputTmpPath);
      }
      ExportConfig.Log("ResProcess.CleanOutputResDir Done");
      return true;
    }
    #endregion

    [MenuItem("Export/Clean/Clean Cache", false, 100)]
    internal static void CleanCacheRes()
    {
      if (ExportConfig.Load()
       && CleanCache()) {
        EditorUtility.DisplayDialog(
          "Confirm",
          "CleanCacheRes Success!",
          "OK");
      } else {
        EditorUtility.DisplayDialog(
          "Confirm",
          "CleanCacheRes Failed!",
          "OK");
      }
    }
  }
}
